package ferus.tigris.lines.personages;

import java.util.ArrayList;
import java.util.List;

import ferus.tigris.lines.Matrix;

import android.graphics.Point;

public class FriendsFinder {
	List<AbstractBehavior> friends = new ArrayList<AbstractBehavior>();

	public FriendsFinder() {
		super();
	}
		
	public List<AbstractBehavior> getFriends(Matrix matrix, Point position, Point vector) {
		if(isInvalidFriend(vector.x, vector.y))return friends;

		AbstractBehavior b = matrix.getMark(position.x, position.y);
		friends.add(b);
		int x = position.x + vector.x;
		int y = position.y + vector.y;

		if(matrix.valid(x, y)) {
			AbstractBehavior friend = matrix.getMark(x, y);
			if(isMyFriend(b, friend)) {
				getFriends(matrix, new Point(x, y), vector);
			}
		}
		return friends;
	}

	private boolean isInvalidFriend(int i, int k) {
		return (i == 0)&&(k == 0);
	}

	private boolean isMyFriend(AbstractBehavior b, AbstractBehavior friend) {
		return friend.getType().equalsIgnoreCase(b.getType())&&isNotInMyFriends(friend)&&!friend.isDied();
	}

	public boolean isNotInMyFriends(AbstractBehavior friend) {
		return !friends.contains(friend);
	}

}